#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

    loadFile();
}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){
    
    for(int x = 0; x < GRID_WIDTH; x++){
        for(int y = 0; y < GRID_HEIGHT; y++){
            ofRect(pos[x][y].x, pos[x][y].y, GRID_SCALE, GRID_SCALE);
        }
    }
}

//--------------------------------------------------------------

void testApp::saveFile(){
	string saveStr = "";
	
    ofFile file("scores.txt",ofFile::WriteOnly);
    
    ofLog(OF_LOG_ERROR, saveStr);
    
    for(int y = 0; y < GRID_HEIGHT; y++){
        for(int x = 0; x < GRID_WIDTH; x++){
            if( pos[x][y].x == -GRID_SCALE){
                file << "-";
            } else {
                file << "X";
            }
        }
        file << "\n";
    }
    
    file.close();

}

void testApp::loadFile(){
	
	ofLog(OF_LOG_ERROR, "LOAD HIGH SCORES:\n");
	
	
	string cInput = "";
    
    ofFile file("scores.txt",ofFile::ReadOnly);
	
    if (file.is_open()) {
		ofLog(OF_LOG_ERROR, "OPEN\n");
        
        for(int y = 0; y < GRID_HEIGHT; y++){
            file >> cInput;
            
            ofLog(OF_LOG_ERROR, cInput);
            
            for(int x = 0; x < GRID_WIDTH; x++){
                char c = cInput.at(x);
                
                if(c != '-'){
                    pos[x][y].set(x * GRID_SCALE, y * GRID_SCALE);
                    ofLog(OF_LOG_ERROR, "char: " + ofToString(c));
                } else {
                    pos[x][y].set(-GRID_SCALE, -GRID_SCALE);
                }
            }
        }
        
		file.close();
		
	} else {
		file.close();
	}

}


//--------------------------------------------------------------
void testApp::keyPressed(int key){
    saveFile();
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
    if( pos[ofGetMouseX()/GRID_SCALE][ofGetMouseY()/GRID_SCALE].x == -GRID_SCALE){
        pos[ofGetMouseX()/GRID_SCALE][ofGetMouseY()/GRID_SCALE].set((ofGetMouseX()/GRID_SCALE) * GRID_SCALE,
                                                                    (ofGetMouseY()/GRID_SCALE) * GRID_SCALE);
    } else {
        pos[ofGetMouseX()/GRID_SCALE][ofGetMouseY()/GRID_SCALE].set(-GRID_SCALE,
                                                                    -GRID_SCALE);
    }

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){ 

}